﻿#ifndef SEARCHDEVICE_H
#define SEARCHDEVICE_H

#include <QDialog>
#include <QPushButton>
#include <QGridLayout>
#include <QThread>
#include <QListWidget>
#include <QTableWidget>
#include <QVBoxLayout>

//#include <QtBluetooth/QLowEnergyService>
//#include <QtBluetooth/QBluetoothSocket>
#include "BTDevice.h"

class SearchDevice : public QDialog
{
    Q_OBJECT
public:
    explicit SearchDevice(BTDevice *bt_device,QDialog *parent = nullptr);

    void write_data_to_device(QByteArray &data);

private:
    QPushButton *btn_search;
    QPushButton *btn_disconnect;
    QGridLayout *layout_grid;
    BTDevice *btDevice;
    QListWidget *list_device;
//    QTableWidget *table_device;
    bool searching;
    QVector<QBluetoothAddress> device_address_list;
    QVector<QString> service_uuid;
//    QDialog dig_service;
    QListWidget *list_service;
    QListWidget *list_characteristic;
    QVBoxLayout *vboxlayout;

    QLowEnergyCharacteristic bleReadChar;
    QLowEnergyCharacteristic bleWriteChar;
    QLowEnergyService *current_service;
    int charCount;



signals:
    void device_is_connect(void);

private slots:
    void on_add_bt_device(const QBluetoothDeviceInfo &info);
    void on_click_btn_search(void);
    void on_device_search_finished(void);
    void on_list_double_clicked(QListWidgetItem *item);
    void on_list_clicked(QListWidgetItem *item);
    void on_service_update(void);
    void on_list_service_double_clicked(QListWidgetItem *item);
    void on_service_dialog_finished(int result);
    void on_service_characteristics_updated(void);
    void on_ble_service_error_occurred(QLowEnergyService::ServiceError newError);
    void on_ble_service_state_changed(QLowEnergyService::ServiceState newState);
    void on_ble_service_characteristic_changed(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue);
    void on_ble_service_descriptor_read(const QLowEnergyDescriptor &descriptor, const QByteArray &value);
    void on_ble_service_descriptor_written(const QLowEnergyDescriptor &descriptor, const QByteArray &newValue);
    void on_ble_service_characteristic_written(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue);
    void on_ble_service_characteristic_read(const QLowEnergyCharacteristic &characteristic, const QByteArray &value);
    void on_ble_device_is_disconnect(const QBluetoothAddress &address);
    void on_btn_disconnect(void);

    void on_tim_read_char(void);

};

#endif // SEARCHDEVICE_H
